package org.loofer.ext.utils;

import org.loofer.ext.component.logger.Logger;
import org.loofer.ext.utils.security.CRC64;
import org.loofer.ext.utils.security.Digest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;

public final class SecurityUtils {
    private static final String TAG = "SecurityUtils";

    private SecurityUtils() {
    }

    public static String digest(String source) {
        return digest(source, "MD5");
    }

    public static String digest(String source, String algorithm) {
        String str = null;
        if (source != null) {
            try {
                str = digestOrThrow(source, algorithm);
            } catch (NoSuchAlgorithmException e) {
                Logger.i(TAG, "fail to digest " + source + " with " + algorithm, e);
            }
        }
        return str;
    }

    public static String digestOrThrow(String source) throws NoSuchAlgorithmException {
        return digestOrThrow(source, "MD5");
    }

    public static String digestOrThrow(String source, String algorithm) throws NoSuchAlgorithmException {
        if (source == null) {
            return null;
        }
        return new Digest(algorithm).digest(source);
    }

    public static String digest(File file) {
        return digest(file, "MD5");
    }

    public static String digest(File file, String algorithm) {
        String str = null;
        if (file != null && file.exists() && file.isFile()) {
            try {
                str = digestOrThrow(file, algorithm);
            } catch (IOException e) {
                Logger.i(TAG, "fail to digest " + file + " with " + algorithm, e);
            } catch (NoSuchAlgorithmException e2) {
                Logger.i(TAG, "fail to digest " + file + " with " + algorithm, e2);
            }
        }
        return str;
    }

    public static String digestOrThrow(File file) throws IOException, NoSuchAlgorithmException {
        return digestOrThrow(file, "MD5");
    }

    public static String digestOrThrow(File file, String algorithm) throws IOException, NoSuchAlgorithmException {
        Throwable th;
        if (file == null) {
            return null;
        }
        InputStream fis = null;
        try {
            InputStream fis2 = new FileInputStream(file);
            try {
                String digest = new Digest(algorithm).digest(fis2);
                if (fis2 == null) {
                    return digest;
                }
                try {
                    fis2.close();
                    return digest;
                } catch (IOException e) {
                    return digest;
                }
            } catch (Throwable th2) {
                th = th2;
                fis = fis2;
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e2) {
                    }
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            if (fis != null) {
                fis.close();
            }
            throw th;
        }
    }

    public static long crc64(byte[] buffer) {
        return CRC64.getValue(buffer);
    }

    public static long crc64(String in) {
        return CRC64.getValue(in);
    }
}
